package com.ff.miaosha.service;

import com.ff.miaosha.dao.MiaoshaUserDao;
import com.ff.miaosha.domain.MiaoshaUser;
import com.ff.miaosha.exception.GlobalException;
import com.ff.miaosha.redis.MiaoshaUserKey;
import com.ff.miaosha.redis.RedisService;
import com.ff.miaosha.result.CodeMsg;
import com.ff.miaosha.util.MD5Util;
import com.ff.miaosha.util.UUIDUtil;
import com.ff.miaosha.vo.LoginVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Service
public class MiaoshaUserService {

    @Autowired
    MiaoshaUserDao miaoshaUserDao;

    @Autowired
    RedisService redisService;

    public static final String COOKIE_NAME_TOKEN="token";


    private MiaoshaUser getById(long id) {
        //取缓存
        MiaoshaUser user=redisService.get(MiaoshaUserKey.getById,""+id,MiaoshaUser.class);
        if (user!=null){
            return user;
        }
        //取数据库
        user=miaoshaUserDao.getById(id);
        if (user!=null){
            redisService.set(MiaoshaUserKey.getById,""+id,user);
        }
        return user;
    }

    public boolean updatePassword(String token,long id,String formPass){
        MiaoshaUser user=getById(id);
        if (user==null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        MiaoshaUser toBeUpdate=new MiaoshaUser();
        toBeUpdate.setId(id);
        toBeUpdate.setPassword(MD5Util.formpassToDBPass(formPass,user.getSalt()));
        miaoshaUserDao.update(toBeUpdate);

        redisService.delete(MiaoshaUserKey.getById,""+id);
        user.setPassword(toBeUpdate.getPassword());
        redisService.set(MiaoshaUserKey.token,token,user);
        return true;
    }
    
    public boolean login(HttpServletResponse response, @Valid LoginVo loginVo) {
        if (loginVo==null){
            throw new GlobalException(CodeMsg.SERVER_ERROR);
        }
        String mobile=loginVo.getMobile();
        String formPass=loginVo.getPassword();
        //判断用户是否存在
        MiaoshaUser user=getById(Long.parseLong(mobile));
        if (user==null){
            throw new GlobalException(CodeMsg.MOBILE_NOT_EXIST);
        }
        //验证密码
        String dbPass=user.getPassword();
        String saltDB=user.getSalt();
        String calcPass= MD5Util.formpassToDBPass(formPass,saltDB);
        if (!calcPass.equals(dbPass)){
            throw new GlobalException(CodeMsg.PASSWORD_ERROR);
        }
        //生成token
        String token= UUIDUtil.uuid();
        addCookie(response,token ,user);
        return true;
    }


    public MiaoshaUser getByToken(HttpServletResponse response, String token) {
        if (StringUtils.isEmpty(token)){
            return null;
        }
        MiaoshaUser user=redisService.get(MiaoshaUserKey.token,token,MiaoshaUser.class);
        //延迟有效期
        if (user!=null){
            addCookie(response,token,user);
        }
        return user;
    }


    private void addCookie(HttpServletResponse response, String token, MiaoshaUser user) {
        redisService.set(MiaoshaUserKey.token,token,user);
        Cookie cookie=new Cookie(COOKIE_NAME_TOKEN,token);
        cookie.setMaxAge(MiaoshaUserKey.token.expiredSeconds());
        cookie.setPath("/");
        response.addCookie(cookie);
    }
}
